package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Withdrawal;
import com.zyd.shiro.business.service.PakWithdrawalService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakWithdrawal;
import com.zyd.shiro.persistence.mapper.PakWithdrawalMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakWithdrawalServiceImpl implements PakWithdrawalService {

    @Autowired
    private PakWithdrawalMapper pakWithdrawalMapper;

    @Override
    public Withdrawal insert(Withdrawal withdrawal) {
        Assert.notNull(withdrawal, "对象不可为空！");
        withdrawal.getPakWithdrawal().setUnicode("KT"+System.currentTimeMillis());
        withdrawal.getPakWithdrawal().setAppstatus(0);
        withdrawal.getPakWithdrawal().setWitstatus(0);
        withdrawal.getPakWithdrawal().setUpdateTime(new Date());
        withdrawal.getPakWithdrawal().setCreateTime(new Date());
        pakWithdrawalMapper.insertSelective(withdrawal.getPakWithdrawal());
        return withdrawal;
    }

    @Override
    public void insertList(List<Withdrawal> withdrawals) {
            for(Withdrawal withdrawal:withdrawals){
               withdrawal.getPakWithdrawal().setUpdateTime(new Date());
               withdrawal.getPakWithdrawal().setCreateTime(new Date());
               pakWithdrawalMapper.insertSelective(withdrawal.getPakWithdrawal());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakWithdrawalMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Withdrawal withdrawal) {
        Assert.notNull(withdrawal, "对象不可为空！");
        withdrawal.setUpdateTime(new Date());
        return pakWithdrawalMapper.updateByPrimaryKey(withdrawal.getPakWithdrawal()) > 0;
    }

    @Override
    public boolean updateSelective(Withdrawal withdrawal) {
        Assert.notNull(withdrawal, "对象不可为空！");
        withdrawal.setUpdateTime(new Date());
        return pakWithdrawalMapper.updateByPrimaryKeySelective(withdrawal.getPakWithdrawal()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Withdrawal getByPrimaryKey(Long primaryKey) {
        PakWithdrawal pakWithdrawal = pakWithdrawalMapper.selectByPrimaryKey(primaryKey);
        return null == pakWithdrawal ? null : new Withdrawal(pakWithdrawal);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Withdrawal getOneByEntity(Withdrawal test) {
        Assert.notNull(test, "对象不可为空！");
        PakWithdrawal pakWithdrawal = pakWithdrawalMapper.selectOne(test.getPakWithdrawal());
        return null == pakWithdrawal ? null : new Withdrawal(pakWithdrawal);
    }

    @Override
    public List<Withdrawal> listAll() {
        List<PakWithdrawal> pakWithdrawals = pakWithdrawalMapper.selectAll();
        if (CollectionUtils.isEmpty(pakWithdrawals)) {
            return null;
        }
        List<Withdrawal> withdrawals = new ArrayList<>();
        for (PakWithdrawal pakWithdrawal : pakWithdrawals) {
            withdrawals.add(new Withdrawal(pakWithdrawal));
        }
        return withdrawals;
    }

    @Override
    public List<Withdrawal> listByEntity(Withdrawal entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Withdrawal> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakWithdrawal> pakWithdrawals = pakWithdrawalMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakWithdrawals)) {
            return null;
        }
        List<Withdrawal> withdrawals = new ArrayList<>();
        for (PakWithdrawal pakWithdrawal : pakWithdrawals) {
            withdrawals.add(new Withdrawal(pakWithdrawal));
        }
        PageInfo bean = new PageInfo<PakWithdrawal>(pakWithdrawals);
        bean.setList(withdrawals);
        return bean;
    }
    @Override
    public void increase(PakWithdrawal pakWithdrawal) {
        pakWithdrawalMapper.insert(pakWithdrawal);
    }




}
